/*
 * Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef SLE_CONNECT_CONFIG_H
#define SLE_CONNECT_CONFIG_H
#include <stdint.h>
#include "sle_device_config.h"
#define SLE_LINK_KEY_LEN 16

typedef enum {
    SLE_CONNECT_OR_DISCONNECT_SUCCESS = 0x00,
    SLE_DISCONNECT_FOR_TIMEOUT = 0x01,
    SLE_DISCONNECT_FOR_REMOTE = 0x10,
    SLE_DISCONNECT_FOR_LOCAL  = 0x11,
    SLE_DISCONNECT_FOR_UNKNOWN_REASON = 0x12,

    SLE_DISCONNECT_WITH_NONE = 0xFF,
} SleDisConnectReason_t;
typedef uint8_t SleDisConnectReason;

#ifndef SleDisConnectReasonStr
#define SleDisConnectReasonStr(disConnectReason)                                                    \
    (disConnectReason == SLE_DISCONNECT_FOR_TIMEOUT) ? "SLE_DISCONNECT_FOR_TIMEOUT" :               \
    (disConnectReason == SLE_DISCONNECT_FOR_REMOTE) ? "SLE_DISCONNECT_FOR_REMOTE" :                 \
    (disConnectReason == SLE_DISCONNECT_FOR_LOCAL) ? "SLE_DISCONNECT_FOR_LOCAL" :                   \
    (disConnectReason == SLE_DISCONNECT_FOR_UNKNOWN_REASON) ? "SLE_DISCONNECT_FOR_UNKNOWN_REASON" : \
    (disConnectReason == SLE_DISCONNECT_WITH_NONE) ? "SLE_DISCONNECT_WITH_NONE" :                   \
    "Unknown"
#endif

typedef enum {
    SLE_CONNECT_IDLE_STATE = 0,                         //与SleAcbState保持一致
    SLE_CONNECT_CONNECTED_STATE = 1,
    SLE_CONNECT_DISCONNECTED_STATE = 2,
    SLE_CONNECT_CONNECTING_STATE = 3,
    SLE_CONNECT_DISCONNECTING_STATE = 4,

    SLE_CONNECT_ERROR_STATE = 0xFF,
} SleConnectState_t;
typedef uint8_t SleConnectState;

#ifndef SleConnectStateStr
#define SleConnectStateStr(connectState)                                                \
    (connectState == SLE_CONNECT_IDLE_STATE) ? "IDLE" :               \
    (connectState == SLE_CONNECT_CONNECTING_STATE) ? "CONNECTING" :   \
    (connectState == SLE_CONNECT_CONNECTED_STATE) ? "CONNECTED" :     \
    (connectState == SLE_CONNECT_DISCONNECTING_STATE) ? "DISCONNECTING" : \
    (connectState == SLE_CONNECT_DISCONNECTED_STATE) ? "DISCONNECTED" :   \
    (connectState == SLE_CONNECT_ERROR_STATE) ? "ERROR" :             \
    "Unknown"
#endif

typedef enum {
    SLE_ACB_NONE_STATE          = 0x00,
    SLE_ACB_CONNECTED_STATE     = 0x01,
    SLE_ACB_DISCONNECTED_STATE  = 0x02,
} SleAcbState_t;
typedef uint8_t SleAcbState;

typedef struct {
    uint16_t intervalMin;
    uint16_t intervalMax;
    uint16_t latency;
    uint16_t supervisionTimeout;
} SleConnectParam;

typedef enum {
    SLE_CRYTO_ALGO_AC1_TYPE     = 0x00,
    SLE_CRYTO_ALGO_AC2_TYPE     = 0x01,
    SLE_CRYTO_ALGO_EA1_TYPE     = 0x02,
    SLE_CRYTO_ALGO_EA2_TYPE     = 0x03,
} SleCryptoAlgo_t;
typedef uint8_t SleCryptoAlgo;

typedef enum {
    SLE_KEY_DERIV_ALGO_HA1_TYPE     = 0x00,
    SLE_KEY_DERIV_ALGO_HA2_TYPE     = 0x01,
} SleKeyDerivAlgo_t;
typedef uint8_t SleKeyDerivAlgo;

typedef enum {
    SLE_ENCRYPTION_ENABLE_INTEGRITY_CHK_ENABLE_IND      = 0x00,
    SLE_ENCRYPTION_DISABLE_INTEGRITY_CHK_ENABLE_IND     = 0x01,
    SLE_ENCRYPTION_ENABLE_INTEGRITY_CHK_DISABLE_IND     = 0x02,
    SLE_ENCRYPTION_DISABLE_INTEGRITY_CHK_DISABLE_IND    = 0x03,
} SleIntegrChkInd_t;
typedef uint8_t SleIntegrChkInd;

typedef struct {
    uint8_t linkKey[SLE_LINK_KEY_LEN];          // 链路密钥
    SleCryptoAlgo cryptoAlgo;                   // 加密算法类型
    SleKeyDerivAlgo keyDerivAlgo;               // 秘钥分发算法类型
    SleIntegrChkInd integrChkInd;               // 完整性校验指示
} SleAuthInfo;

/** hdi config structure */

/**
 * @if Eng
 * @brief Enum of Sle logical link update parameters.
 * @else
 * @brief 星闪逻辑链路更新参数
 * @endif
 */
typedef struct {
    uint16_t connectId;             /*!< @if Eng connection ID
                                       @else   连接ID @endif */
    uint16_t intervalMin;        /*!< @if Eng minimum interval
                                       @else   链路调度最小间隔，单位slot @endif */
    uint16_t intervalMax;        /*!< @if Eng maximum interval
                                       @else   链路调度最大间隔，单位slot @endif */
    uint16_t maxLatency;         /*!< @if Eng maximum latency
                                       @else   延迟周期，单位slot @endif */
    uint16_t supervisionTimeout; /*!< @if Eng timeout
                                       @else   超时时间，单位10ms @endif */
} SleConnectionParamUpdate;

/**
 * @if Eng
 * @brief Enum of Sle logical link update event parameters.
 * @else
 * @brief 星闪逻辑链路更新事件参数
 * @endif
 */
typedef struct {
    uint16_t interval;              /*!< @if Eng interval
                                         @else   链路调度间隔，单位slot @endif */
    uint16_t latency;               /*!< @if Eng latency
                                         @else   延迟周期，单位slot @endif */
    uint16_t supervisionTimeout;           /*!< @if Eng timeout
                                         @else   超时时间，单位10ms @endif */
} SleConnectionParamUpdateEvt;

/**
 * @if Eng
 * @brief Enum of Sle logical link update event parameters.
 * @else
 * @brief 星闪逻辑链路更新事件参数
 * @endif
 */
typedef struct {
    uint8_t tx_format;          /*!< @if Eng Transmitted radio frame type, @ref sle_radio_frame_t
                                     @else 发送无线帧类型，参考 { @ref sle_radio_frame_t }。 @endif */
    uint8_t rx_format;          /*!< @if Eng Received radio frame type, @ref sle_radio_frame_t
                                     @else 接收无线帧类型，参考 { @ref sle_radio_frame_t }。 @endif */
    uint8_t tx_phy;             /*!< @if Eng Transmitted PHY, @ref sle_phy_tx_rx_t
                                     @else 发送PHY，参考 { @ref sle_phy_tx_rx_t }。 @endif */
    uint8_t rx_phy;             /*!< @if Eng Received PHY, @ref sle_phy_tx_rx_t
                                     @else 接收PHY，参考 { @ref sle_phy_tx_rx_t }。 @endif */
    uint8_t tx_pilot_density;   /*!< @if Eng Transmitted pilot density indicator, @ref sle_phy_tx_rx_pilot_density_t
                                     @else 发送导频密度指示，参考 { @ref sle_phy_tx_rx_pilot_density_t }。 @endif */
    uint8_t rx_pilot_density;   /*!< @if Eng Received pilot density indicator, @ref sle_phy_tx_rx_pilot_density_t
                                     @else 接收导频密度指示，参考 { @ref sle_phy_tx_rx_pilot_density_t }。 @endif */
    uint8_t g_feedback;         /*!< @if Eng Indicates the feedback type of the pre-transmitted link.
                                             The value range is 0 to 63.
                                     @else 先发链路反馈类型指示，取值范围0-63。 @endif */
    uint8_t t_feedback;         /*!< @if Eng Indicates the feedback type of the post-transmit link.
                                             The value range is 0-7.
                                     @else 后发链路反馈类型指示，取值范围0-7。 @endif */
} SleConnectionPhyParam;

typedef enum {
    SLE_PAIR_NONE_STATE    = 0x01,
    SLE_PAIR_PAIRING_STATE = 0x02,
    SLE_PAIR_PAIRED_STATE  = 0x03,

    SLE_PAIR_ERROR_STATE   = 0xFF,
} SlePairState_t;
typedef uint8_t SlePairState;

#ifndef SlePairStateStr
#define SlePairStateStr(pairState)                                                  \
    (pairState) == SLE_PAIR_NONE_STATE ? "SLE_PAIR_NONE_STATE" :                     \
    (pairState) == SLE_PAIR_PAIRING_STATE ? "SLE_PAIR_PAIRING_STATE" :                \
    (pairState) == SLE_PAIR_PAIRED_STATE ? "SLE_PAIR_PAIRED_STATE" :                   \
    (pairState) == SLE_PAIR_ERROR_STATE ? "SLE_PAIR_ERROR_STATE" : "Unknown"
#endif

typedef struct {
    uint16_t connId;
    SleDeviceAddress addr;
} SleConnectDev;

#define SLE_MAX_CONNECT_ID_NUM  20
#endif //SLE_CONNECT_CONFIG_H